﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class Ad
    {
        public int Add(KeRui.Model.Ad model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AdName", SqlDbType.NVarChar, 50), new SqlParameter("@AdWidth", SqlDbType.Int, 4), new SqlParameter("@AdHight", SqlDbType.Int, 4), new SqlParameter("@AdType", SqlDbType.Int, 4), new SqlParameter("@BuyNum", SqlDbType.Int, 4), new SqlParameter("@BuyUnit", SqlDbType.Int, 4), new SqlParameter("@IsUsing", SqlDbType.Bit, 1), new SqlParameter("@siteID", SqlDbType.Int, 4), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15), new SqlParameter("@DefaultAdText", SqlDbType.NVarChar, 0xff), new SqlParameter("@PersonInCharge", SqlDbType.NVarChar, 0xff),new SqlParameter("@SelFalg",SqlDbType.NVarChar,50) };
            parameters[0].Value = model.AdName;
            parameters[1].Value = model.AdWidth;
            parameters[2].Value = model.AdHight;
            parameters[3].Value = model.AdType;
            parameters[4].Value = model.BuyNum;
            parameters[5].Value = model.BuyUnit;
            parameters[6].Value = model.IsUsing;
            parameters[7].Value = model.SiteID;
            parameters[8].Value = model.CreateIP;
            parameters[9].Value = model.DefaultAdText;
            parameters[10].Value = model.PersonInCharge;
            parameters[11].Value = model.SelFalg;
            return DbHelperSQL.RunProcedure("Ad_ADD", parameters, out num);
        }

        public int Delete(int AdID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AdID", SqlDbType.Int, 4) };
            parameters[0].Value = AdID;
            return DbHelperSQL.RunProcedure("Ad_Delete", parameters, out num);
        }

        public KeRui.Model.Ad GetModel(int AdID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AdID", SqlDbType.Int, 4) };
            parameters[0].Value = AdID;
            KeRui.Model.Ad ad = new KeRui.Model.Ad();
            DataSet set = DbHelperSQL.RunProcedure("Ad_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["AdID"].ToString() != "")
                {
                    ad.AdID = int.Parse(set.Tables[0].Rows[0]["AdID"].ToString());
                }
                if (set.Tables[0].Rows[0]["SiteID"].ToString() != "")
                {
                    ad.SiteID = int.Parse(set.Tables[0].Rows[0]["SiteID"].ToString());
                }
                ad.AdName = set.Tables[0].Rows[0]["AdName"].ToString();
                if (set.Tables[0].Rows[0]["AdWidth"].ToString() != "")
                {
                    ad.AdWidth = int.Parse(set.Tables[0].Rows[0]["AdWidth"].ToString());
                }
                if (set.Tables[0].Rows[0]["AdHight"].ToString() != "")
                {
                    ad.AdHight = int.Parse(set.Tables[0].Rows[0]["AdHight"].ToString());
                }
                if (set.Tables[0].Rows[0]["AdType"].ToString() != "")
                {
                    ad.AdType = int.Parse(set.Tables[0].Rows[0]["AdType"].ToString());
                }
                if (set.Tables[0].Rows[0]["CurID"].ToString() != "")
                {
                    ad.CurID = int.Parse(set.Tables[0].Rows[0]["CurID"].ToString());
                }
                ad.CurUrl = set.Tables[0].Rows[0]["CurUrl"].ToString();
                if (set.Tables[0].Rows[0]["CurSellerID"].ToString() != "")
                {
                    ad.CurSellerID = int.Parse(set.Tables[0].Rows[0]["CurSellerID"].ToString());
                }
                ad.CurSellerName = set.Tables[0].Rows[0]["CurSellerName"].ToString();
                ad.CurAdText = set.Tables[0].Rows[0]["CurAdText"].ToString();
                if (set.Tables[0].Rows[0]["CurStartDate"].ToString() != "")
                {
                    ad.CurStartDate = DateTime.Parse(set.Tables[0].Rows[0]["CurStartDate"].ToString());
                }
                if (set.Tables[0].Rows[0]["CurEndDate"].ToString() != "")
                {
                    ad.CurEndDate = DateTime.Parse(set.Tables[0].Rows[0]["CurEndDate"].ToString());
                }
                if (set.Tables[0].Rows[0]["BuyNum"].ToString() != "")
                {
                    ad.BuyNum = int.Parse(set.Tables[0].Rows[0]["BuyNum"].ToString());
                }
                if (set.Tables[0].Rows[0]["BuyUnit"].ToString() != "")
                {
                    ad.BuyUnit = int.Parse(set.Tables[0].Rows[0]["BuyUnit"].ToString());
                }
                if (set.Tables[0].Rows[0]["IsUsing"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["IsUsing"].ToString() == "1") || (set.Tables[0].Rows[0]["IsUsing"].ToString().ToLower() == "true"))
                    {
                        ad.IsUsing = true;
                    }
                    else
                    {
                        ad.IsUsing = false;
                    }
                }
                if (set.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    ad.State = int.Parse(set.Tables[0].Rows[0]["State"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    ad.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                ad.CreateIP = set.Tables[0].Rows[0]["CreateIP"].ToString();
                ad.DefaultAdText = set.Tables[0].Rows[0]["DefaultAdText"].ToString();
                ad.PersonInCharge = set.Tables[0].Rows[0]["PersonInCharge"].ToString();
                return ad;
            }
            return null;
        }

        public int Update(KeRui.Model.Ad model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AdID", SqlDbType.Int, 4), new SqlParameter("@AdName", SqlDbType.NVarChar, 50), new SqlParameter("@AdWidth", SqlDbType.Int, 4), new SqlParameter("@AdHight", SqlDbType.Int, 4), new SqlParameter("@AdType", SqlDbType.Int, 4), new SqlParameter("@BuyNum", SqlDbType.Int, 4), new SqlParameter("@BuyUnit", SqlDbType.Int, 4), new SqlParameter("@siteID", SqlDbType.Int, 4), new SqlParameter("@IsUsing", SqlDbType.Bit, 1), new SqlParameter("@DefaultAdText", SqlDbType.NVarChar, 0xff), new SqlParameter("@PersonInCharge", SqlDbType.NVarChar, 0xff), new SqlParameter("@SelFalg", SqlDbType.NVarChar, 50) };
            parameters[0].Value = model.AdID;
            parameters[1].Value = model.AdName;
            parameters[2].Value = model.AdWidth;
            parameters[3].Value = model.AdHight;
            parameters[4].Value = model.AdType;
            parameters[5].Value = model.BuyNum;
            parameters[6].Value = model.BuyUnit;
            parameters[7].Value = model.SiteID;
            parameters[8].Value = model.IsUsing;
            parameters[9].Value = model.DefaultAdText;
            parameters[10].Value = model.PersonInCharge;
            parameters[11].Value = model.SelFalg;
            return DbHelperSQL.RunProcedure("Ad_Update", parameters, out num);
        }

        public int UpdateForSellerAd(int AdID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AdID", SqlDbType.Int, 4) };
            parameters[0].Value = AdID;
            return DbHelperSQL.RunProcedure("Ad_UpdateForSellerAd", parameters, out num);
        }
    }
}

